<?php

/**
 * This is the model class for table "user_tb".
 *
 * The followings are the available columns in table 'user_tb':
 * @property integer $uid
 * @property string $username
 * @property string $pass
 * @property string $email
 * @property string $address
 * @property string $phone
 * @property integer $groupid
 * @property string $registerdate
 * @property integer $flag
 */
class UserTb extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user_tb';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, pass, email, address, phone, registerdate,cmnd,birthday,sex, locaID, cityID', 'required'),
            array('groupid, flag,sex, locaID, cityID', 'numerical', 'integerOnly' => true),
            array('username, pass, email', 'length', 'max' => 100),
            array('address', 'length', 'max' => 200),
            array('phone', 'length', 'max' => 20),
            array('mota', 'length', 'max'=>1000),
            array('email', 'email', 'message' => 'Email không đúng định dạng'),
            array('email', 'unique', 'message' => 'Email này đã tồn tại'),
            array('cmnd', 'length', 'max' => 10),
            array('cmnd', 'unique', 'message' => 'CMND này đã tồn tại'),
         
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('uid, username, pass, email, address, phone, groupid, registerdate, flag, cmnd, birthday', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'uid' => 'Uid',
            'username' => 'Họ và tên',
            'pass' => 'Mật khẩu',
            'email' => 'Email',
            'address' => 'Địa chỉ',
            'phone' => 'Điện thoại',
            'groupid' => 'Groupid',
            'registerdate' => 'Registerdate',
            'flag' => 'Flag',
            'cmnd' => 'CMND',
            'birthday' => 'Ngày sinh',
            'sex' => 'Giới tính', 
            'locaID' => 'Đất nước', 
            'cityID' => 'Thành phố',
            'mota' => 'Mô tả'
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('uid', $this->uid);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('pass', $this->pass, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('groupid', $this->groupid);
        $criteria->compare('registerdate', $this->registerdate, true);
        $criteria->compare('flag', $this->flag);
        $criteria->compare('cmnd',$this->cmnd,true);
        $criteria->compare('birthday',$this->birthday,true);
        $criteria->compare('sex',$this->sex,true);
        $criteria->compare('contry',$this->contry,true);
        $criteria->compare('city',$this->city,true);
        $criteria->compare('mota',$this->mota,true);
        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return UserTb the static model class
     */
     public function findByEmail($email) {
        return $this->find('email=:email', array(':email' => $email));
    }
    
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

}
